package com.user.seat.service.impl;

import com.user.seat.dao.UserSeatDao;
import com.user.seat.domain.ResultInfo;
import com.user.seat.domain.SeatNumber;
import com.user.seat.domain.SeatUser;
import com.user.seat.service.UserSeatService;
import com.user.seat.utils.JwtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

/**
 * @author jimmy
 * Date  2022/4/21 14:41
 * @version 1.0
 */
@Service
public class UserSeatServiceImpl implements UserSeatService {

    private final UserSeatDao userSeatDao;
    private final JwtService jwtService;

    @Autowired
    public UserSeatServiceImpl(UserSeatDao userSeatDao,
                               JwtService jwtService){
        this.userSeatDao = userSeatDao;
        this.jwtService =jwtService;
    }


    @Override
    public ResultInfo doLogin(SeatUser seatUser) {
        //验证
        List<SeatUser> seatUserList =  userSeatDao.selectUser(seatUser);

        ResultInfo resultInfo = verifyUser(seatUser,seatUserList);

        return resultInfo;
    }

    @Override
    public ResultInfo doRegister(SeatUser seatUser) {
        ResultInfo resultInfo = new ResultInfo();
        List<SeatUser> seatUserList =  userSeatDao.selectUser(seatUser);
        if (seatUserList.size()>0){
            resultInfo.setCode(400);
            resultInfo.setMsg("用户已存在");
            resultInfo.setStatus("-1");
            resultInfo.setCode(400);
        }else {
           int id = userSeatDao.registerUser(seatUser);
            resultInfo.setCode(200);
            resultInfo.setMsg("注册成功，请重新登录");
            resultInfo.setStatus("1");
            resultInfo.setToken(jwtService.createToken(id+"",seatUser.getUserName()));

        }
        return resultInfo;

    }

    @Override
    public ResultInfo doUpdateNikName(SeatUser seatUser) {
        ResultInfo resultInfo = new ResultInfo();
        int id = userSeatDao.updateNikName(seatUser);
        resultInfo.setCode(200);
        resultInfo.setMsg("成功");
        resultInfo.setBody(seatUser);
        return resultInfo;
    }

    public ResultInfo verifyUser(SeatUser seatUser,List<SeatUser> seatUserList){
        ResultInfo resultInfo = new ResultInfo();
        if (seatUserList.size()<1){
            resultInfo.setMsg("用户不存在");
            resultInfo.setCode(403);
            return resultInfo;
        }
        SeatUser verifyUser = seatUserList.get(0);
        String md5Password = "";
        if (seatUser.getPassword()!=null){
            md5Password = DigestUtils.md5DigestAsHex(seatUser.getPassword().getBytes());
        }
        //1.校验是否存在
        if (verifyUser!=null){
            //2.校验密码
            if (md5Password.equalsIgnoreCase(verifyUser.getPassword())){
                resultInfo.setCode(200);
                SeatUser resultUser = new SeatUser();
                resultUser.setUserName(verifyUser.getUserName());
                resultUser.setNikName(verifyUser.getNikName());
                resultUser.setId(verifyUser.getId());
                resultInfo.setBody(resultUser);
                resultInfo.setToken(jwtService.createToken(verifyUser.getId()+"",verifyUser.getUserName()));
            }else {
                resultInfo.setMsg("密码错误");
                resultInfo.setCode(403);
            }

        }else {
            resultInfo.setMsg("用户不存在");
            resultInfo.setCode(403);
        }

        return resultInfo;
    }


}
